package data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
/**
 * A user list store the name of users who are currently connected to the server.
 * @author Jie Zhao(288654)
 *
 */
public class UserList extends Observable {
	private static UserList instance = null;

	private UserList() {
	}

	/**
	 * 
	 * @return an instance of user list
	 */
	public static UserList getInstance() {
		if (instance == null) {
			instance = new UserList();
		}
		return instance;
	}

	private List<String> clientList = Collections.synchronizedList(new ArrayList<String>());

	/**
	 * clear the user list
	 */
	public synchronized void clear() {
		clientList.clear();
		setChanged();
		notifyObservers();
	}
/**
 * remove a client from the list
 * @param username
 */
	public synchronized void removeClient(String username) {
		if (username != null)
		{
			clientList.remove(username);
			setChanged();
			notifyObservers();
		}
	}

	/**
	 * Add a client to the list
	 * @param name
	 */
	public synchronized void  addClient(String name) {
		clientList.add(name);
		setChanged();
		notifyObservers();
	}
	/**
	 * get the user name list 
	 * @return
	 */
	public List<String> getClientList()
	{
		return clientList;
	}
	
	/**
	 * print the user to the console
	 */
	public synchronized void print()
	{
		for(String u:clientList)
		{
			System.out.print(u+"|");
		}
		System.out.println();
	}
}